//
//  NavHeaderView.m
//  hnGovernment
//
//  Created by 安仔 on 17/2/25.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "NavHeaderView.h"

@implementation NavHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
   
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = APPColor;
        [self addSubview:self.cityBtn];
        [self addSubview:self.searchBar];
        [self addSubview:self.messageBtn];
        [self addSubview:self.msgNumLab];

    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    [self.cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18 * m6Scale);
        make.top.mas_equalTo(45*m6Scale);
        make.size.mas_equalTo(CGSizeMake(174*m6Scale, 60*m6Scale));
    }];
    
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.cityBtn.mas_centerY);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(440*m6Scale, 60*m6Scale));
    }];
    
    [self.messageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.cityBtn.mas_centerY);
        make.right.mas_equalTo(-32*m6Scale);
        make.size.mas_equalTo(CGSizeMake(56*m6Scale, 32*m6Scale));
    }];
    
    [self.msgNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.messageBtn.mas_right).offset(-20*m6Scale);
        make.centerY.mas_equalTo(self.messageBtn.mas_centerY).offset(-18*m6Scale);
        make.size.mas_equalTo(CGSizeMake(28*m6Scale, 28*m6Scale));
    }];
    
}


- (FL_Button *)cityBtn {
    
    if (!_cityBtn) {
        _cityBtn = [FL_Button fl_shareButton];
        _cityBtn.backgroundColor = [UIColor clearColor];
        [_cityBtn setImage:[UIImage imageNamed:@"nar_home_down arrow"] forState:0];
        [_cityBtn setTitle:NSLocalString(@"郑州市") forState:0];
        [_cityBtn setTitleColor:[UIColor whiteColor] forState:0];
        _cityBtn.titleLabel.font = [UIFont systemFontOfSize:25*m6Scale];
        _cityBtn.status = FLAlignmentStatusLeft;
        [_cityBtn addTarget:self action:@selector(chooseCity) forControlEvents:UIControlEventTouchUpInside];

    }
    return _cityBtn;
}

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] init];
        _searchBar.placeholder = NSLocalString(@"郑州出台房屋征收与补偿方法");
        
//        _searchBar.barStyle = UIBarStyleBlack;
        UITextField *searchField =  [_searchBar valueForKey:@"searchField"];
        // 默认文本颜色
        [searchField setValue:[UIColor colorWithWhite:1.0 alpha:0.55] forKeyPath:@"_placeholderLabel.textColor"];
        [searchField setValue:[UIFont systemFontOfSize:24*m6Scale]forKeyPath:@"_placeholderLabel.font"];
        searchField.textColor = [UIColor whiteColor];
        [[[[ _searchBar.subviews objectAtIndex : 0 ] subviews ] objectAtIndex : 0 ] removeFromSuperview ];
        _searchBar.subviews.firstObject.subviews.firstObject.subviews.firstObject.hidden = YES;
        //放大镜
        [_searchBar setImage:[UIImage imageNamed:@"nar_home_search"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        _searchBar.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.22];
        _searchBar.layer.cornerRadius = 10.0*m6Scale;
        _searchBar.clipsToBounds = YES;
    }
    return _searchBar;
}


- (UIButton *)messageBtn {
    if (!_messageBtn) {
        _messageBtn = [[UIButton alloc] init];
        _messageBtn.backgroundColor = [UIColor clearColor];
        [_messageBtn setImage:[UIImage imageNamed:@"Group"] forState:0];
        [_messageBtn addTarget:self action:@selector(checkHomePageMessage) forControlEvents:UIControlEventTouchUpInside];

    }
    return _messageBtn;
}

//信息数量
- (UILabel *)msgNumLab {
    if (!_msgNumLab) {
        _msgNumLab = [[UILabel alloc] init];
        _msgNumLab.backgroundColor = [UIColor colorWithHexString:@"ff5e00"];
        _msgNumLab.textColor = [UIColor whiteColor];
        _msgNumLab.font = [UIFont systemFontOfSize:20*m6Scale];
        _msgNumLab.textAlignment = NSTextAlignmentCenter;
        _msgNumLab.layer.cornerRadius = 14*m6Scale;
        _msgNumLab.clipsToBounds = YES;
        _msgNumLab.layer.borderWidth = 2.0*m6Scale;
        _msgNumLab.layer.borderColor = [UIColor whiteColor].CGColor;
        _msgNumLab.text = @"2";
    }
    return _msgNumLab;
}

#pragma mark - 按钮响应方法

- (void)chooseCity {
    if ([self.delegate respondsToSelector:@selector(chooseCity)]) {
        [self.delegate chooseCity];
    }
}

- (void)checkHomePageMessage {
    if ([self.delegate respondsToSelector:@selector(checkHomePageMessage)]) {
        [self.delegate checkHomePageMessage];
    }
}
@end
